<%@ page import="java.sql.*" %>
<%@ page import="com.xxx.entity.User" %>
<%@ page import="com.xxx.DAO.booksDao" %>
<%@ page import="com.xxx.util.pageUtil" %>
<%@ page import="java.util.List" %>
<%@ page import="com.xxx.entity.Book" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.xxx.util.JDBCUtil" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>图书资源页面</title>
    <link rel="stylesheet" type="text/css" href="css/catalog1.css"/>
</head>
<body id="catalog_body">
<div id="catalog">
    <div id="catalog_header">
        <div id="catalog_nav">
            导航: <a href="Home.jsp">首页</a> > <a href="person.jsp">我的</a>
        </div>
        <div id="catalog_search">
            <form action="Catalog3.jsp" method="post">
                <input type="text" name="search" id="search" value="" />
                <button type="submit">搜索</button>
            </form>
        </div>
    </div>
    <div id="content">
        <div id="catalog_left">
            <div id="classfiy">
                图书分类
            </div>
            <ul>
                <li><a href="#">文字</a></li>
                <li><a href="#">政治、法律</a></li>
                <li><a href="#">经济</a></li>
                <li><a href="#">历史、地理</a></li>
                <li><a href="#">文化、科学、教育、体育</a></li>
                <li><a href="#">哲学、宗教</a></li>
                <li><a href="#">社会科学总论</a></li>
                <li><a href="#">语言、文字</a></li>
                <li><a href="#">艺术</a></li>
                <li><a href="#">工业技术</a></li>
                <li><a href="#">马克思主义、列宁主义、毛泽东思想、邓小平理论</a></li>
                <li><a href="#">地球科学</a></li>
                <li><a href="#">环境科学、安全科学</a></li>
                <li><a href="#">医药、卫生</a></li>
                <li><a href="#">军事</a></li>
                <li><a href="#">农业科学</a></li>
                <li><a href="#">综合性科学</a></li>
                <li><a href="#">数理科学和化学</a></li>
                <li><a href="#">自然科学总论</a></li>
                <li><a href="#">生物科学</a></li>
                <li><a href="#">交通运输</a></li>
                <li><a href="#">航天、航空</a></li>
            </ul>
        </div>
        <div id="catalog_right">
            <table width="650px" border="0" align="center" cellpadding="4" cellspacing="1" id="table1">
                <tbody>
                <tr>
                    <th>书名</th>
                    <th>作者</th>
                    <th>出版社</th>
                    <th>剩余数量</th>
                    <th>操作</th>
                </tr>
                <%--连接数据库--%>
                <%
                    int iPageSize = 4, iTotal = 0, iPageCnt = 0, iPage = 0;
                    if (request.getParameter("page") != null) {// 从翻页操作链接进入
                        try {
                            iPage = Integer.parseInt(request.getParameter("page"));
                        } catch (RuntimeException e) {
                            out.println("类型转换异常");
                        }
                    }
                    if (iPage < 1) iPage = 1;
                    booksDao dao=new booksDao();//创建booksDao类
                    List<Book> booksList = new ArrayList<>();
                    List<Book> booksList2 = new ArrayList<>();
                    pageUtil pageUtil=new pageUtil();//创建分页类
                    JDBCUtil util=new JDBCUtil();
                    if (request.getParameter("search")!=null) {
                        String search = new String(request.getParameter("search").getBytes("iso-8859-1"), "UTF-8");
                        booksList=dao.select(search);//生成查询结果book类的list
                        iTotal = pageUtil.getPageCount(booksList);
                        iPageCnt=(iTotal+iPageSize-1)/iPageSize;
                        if (iPage>iPageCnt)iPage=iPageCnt;
                        if (iPageCnt>0) {
                            booksList2 = pageUtil.startPage(booksList, iPage, 4);//分页后的book类list
                            for (Book obj : booksList2) {
                                out.print("<tr>");
                                out.print("<td>" +"<img src=\""+obj.getPath()+"\"/> "+"<br>"+obj.getbName()+"</td>");
                                out.print("<td>" + obj.getbAut() + "</td>");
                                out.print("<td>" + obj.getbPub() + "</td>");
                                out.print("<td>" + obj.getbSum() + "</td>");
                                PreparedStatement ps = util.createPstmt("select DISTINCT bName from user_book ub  left join tb_user u on ub.uid=u.userId left join books b on ub.bid=b.id where u.userId=" + request.getSession().getAttribute("id") + " and bName like '" + obj.getbName() + "'");
                                ResultSet rs = null;
                                try {
                                    rs = ps.executeQuery();
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }
//								rs2=stmt.executeQuery("select DISTINCT bName from user_book ub  left join tb_user u on ub.uid=u.userId left join books b on ub.bid=b.id where u.userId="+request.getSession().getAttribute("id")+" and bName like '"+rs.getString("bName")+"'");
                                try {
                                    if (rs.next()) {
                                        out.print("<td><a>已借</a></td>");
                                    } else {
                                        out.print("<td><a href='jieshu?id=" + obj.getId() + "'>借书</a></td>");
                                    }
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }
                                out.print("</tr>");

                            }
                        }

                    }else{
                        booksList=dao.findAll();
                        iTotal = pageUtil.getPageCount(booksList);
                        iPageCnt=(iTotal+iPageSize-1)/iPageSize;
                        if (iPage>iPageCnt)iPage=iPageCnt;
                        if (iPageCnt>0) {
                            booksList2 = pageUtil.startPage(booksList, iPage, 4);
                            for (Book obj : booksList2) {
                                out.print("<tr>");
                                out.print("<td>" +"<img src=\""+obj.getPath()+"\"/> "+"<br>"+obj.getbName()+"</td>");
                                out.print("<td>" + obj.getbAut() + "</td>");
                                out.print("<td>" + obj.getbPub() + "</td>");
                                out.print("<td>" + obj.getbSum() + "</td>");
                                PreparedStatement ps = util.createPstmt("select DISTINCT bName from user_book ub  left join tb_user u on ub.uid=u.userId left join books b on ub.bid=b.id where u.userId=" + request.getSession().getAttribute("id") + " and bName like '" + obj.getbName() + "'");
                                ResultSet rs = null;
                                try {
                                    rs = ps.executeQuery();
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }
//								rs2=stmt.executeQuery("select DISTINCT bName from user_book ub  left join tb_user u on ub.uid=u.userId left join books b on ub.bid=b.id where u.userId="+request.getSession().getAttribute("id")+" and bName like '"+rs.getString("bName")+"'");
                                try {
                                    if (rs.next()) {
                                        out.print("<td><a>已借</a></td>");
                                    } else {
                                        out.print("<td><a href='jieshu?id=" + obj.getId() + "'>借书</a></td>");
                                    }
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }
                                out.print("</tr>");

                            }
                        }

                    }%>
                <tr align="center">
                    <th colspan="8">
                        当前页数<%=iPage%>/<%=iPageCnt%>&nbsp;
                        <%if (iPage > 1) {%>
                        <a href="Catalog3.jsp?page=1">第一页</a>
                        <a href="Catalog3.jsp?page=<%=iPage-1%>">上一页</a>
                        <%} if (iPage < iPageCnt) { %>
                        <a href="Catalog3.jsp?page=<%=iPage+1%>">下一页</a>
                        <a href="Catalog3.jsp?page=<%=iPageCnt%>">页尾</a>
                        <%}%>
                    </th>
                </tr>
                </tbody>
            </table>
        </div>

    </div>
</div>
</body>
</html>



